package com.woniu.cq.medical.common;



import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.util.*;

/**
 * 时间工具类
 */
public class DateUtil {

    /**
     * 把得到的时间时间转换未字符串
     * @param date
     * @return
     */
    public static String getWeekOfDate(Date date) {
        String[] weekDays = { "周日", "周一", "周二", "周三", "周四", "周五", "周六" };
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        int w = cal.get(Calendar.DAY_OF_WEEK) - 1;
        if (w < 0)
            w = 0;
        return weekDays[w];
    }

    /**
     * 往后延续7天
     * @return
     */
    public static Date getWeekSunday() throws ParseException {
        Calendar cal = Calendar.getInstance();
        Date time = cal.getTime();
        cal.add(Calendar.DATE,7);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String s = sdf.format(cal.getTime());
        Date sunday =  sdf.parse(s);
        return sunday;
    }

    /**
     * 如果已经是下午5点则加一天 否则返回当天
     * @param date
     * @return
     */
    public static Date getCurrentTime(Date date) throws ParseException {
        Calendar cal = Calendar.getInstance();
        int time = cal.get(Calendar.HOUR_OF_DAY);
        if (time>=17){
           cal.add(Calendar.DATE,1);
        }
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd ");
        String s = sdf.format(cal.getTime());
        Date CurrentTime =  sdf.parse(s);
        return CurrentTime;
    }

    /**
     * 根据时间转换成字符串
     * @param dates
     * @return
     */
    public static Map<String,String> getDataWeek(List<Date> dates){
        Map<String,String> map = new HashMap<>();
        for (Date date: dates) {
            Calendar cal=Calendar.getInstance();
            cal.setTime(date);
            int week =cal.get(Calendar.DAY_OF_WEEK);
            switch (week-1){
                case 1:
                    map.put("1","周一");
                    break;
                case 2:
                    map.put("2","周二");
                    break;
                case 3:
                    map.put("3","周三");
                    break;
                case 4:
                    map.put("4","周四");
                    break;
                case 5:
                    map.put("5","周五");
                    break;
                case 6:
                    map.put("6","周六");
                    break;
                case 0:
                    map.put("7","周日");
                    break;
                default:
                    break;
            }
        }
        return map;
    }

    /**
     * 对取过来的值进行比对,确定明确的值
     * @param value
     * @param dates
     * @return
     */
    public static Date getDayDate(Integer value,List<Date> dates){
        for (Date date: dates) {
            Calendar cal = Calendar.getInstance();
            cal.setTime(date);
            int week = cal.get(Calendar.DAY_OF_WEEK)-1;
            System.out.println(week);
            if (week==0){
                week=7;
            }
            if (value==week){
                return date;
            }
        }
        return null;
    }

    /**
     * Date 转换为 LocalDate
     * @param date
     * @return
     */
    public static LocalDate getLocalDate(Date date) {
        if(null == date) {
            return null;
        }
        return date.toInstant().atZone(ZoneId.systemDefault()).toLocalDate();
    }

    /**
     * LocalDate 转换为 Date
     * @param localDate
     * @return
     */
    public static Date getDate(LocalDate localDate) {
        if(null == localDate) {
            return null;
        }
        ZonedDateTime zonedDateTime = localDate.atStartOfDay(ZoneId.systemDefault());
        return Date.from(zonedDateTime.toInstant());
    }

    /**
     * 获取本月开始和结束日期
     * @param date
     * @return
     */
    public static List<Date> getThisMonth(Date date){
        List<Date> dates = new ArrayList<>();
        final Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        //当前月第一天
        cal.add(Calendar.MONTH, 0);
        cal.set(Calendar.DAY_OF_MONTH, 1);
        Date startTime = cal.getTime();
        dates.add(startTime);
        cal.add(Calendar.MONTH, 1);
        cal.set(Calendar.DAY_OF_MONTH, 0);
        Date endTime = cal.getTime();
        dates.add(endTime);
        return dates;

    }

}
